<?php
namespace app\admin\service;
use app\admin\model\Config;
use app\admin\service\interface\ConfigService;
class ConfigServiceImpl implements ConfigService{
    public function add(string $key,string $data,string $type){
        $config = new Config();
        $config->key=$key;
        $config->data=$data;
        $config->type=$type;
        $config->save();
    }
    public function all():array{
        return [];
    }
    public function delete(string $key){
        $config = Config::where('key',$key)->findOrEmpty();
        if ($config->isEmpty()) {
            throw new \Exception('配置项不存在！');
        }
        return $config->delete();
    }
    public function buildCacheData():array{
        $res=[];
        $arr=Config::column("*","key");
        foreach ($arr as $key => $value) {
            $res[$key] = match (trim($value['type'])) {
                 'int' => (int)$value['data'] ,
                  default =>$value['data'],
            };
        }
        return $res;
    }
}